package com.example.day02.home;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.example.day02.MainActivity;
import com.example.day02.R;

public class HomeActivity extends AppCompatActivity {
    TextView countTv;
    int second=5;
    SharedPreferences preferences;//存储价值对数据
    private SharedPreferences.Editor edit;
    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==1) {
                second--;
                if (second == 0) {
                    Intent intent = new Intent();
                    boolean isFirst = preferences.getBoolean("isFirst", true);
                    if (isFirst) {
                        intent.setClass(HomeActivity.this, GuideActivity.class);
                        edit.putBoolean("isFirst",false);
                        edit.commit();
                    }else {
                        intent.setClass(HomeActivity.this,MainActivity.class);
                    }
                    startActivity(intent);
                    finish();
                }else {
                    countTv.setText(second+"");
                    handler.sendEmptyMessageDelayed(1,1000);//延迟发送消息
                }
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        countTv=findViewById(R.id.home_tv);
       handler.sendEmptyMessageDelayed(1,1000);//延迟发送消息
        preferences=getSharedPreferences("setting_pref",MODE_PRIVATE);//获取共享参数对象
        edit = preferences.edit();//写入数据的对象
    }

}
